package com.fire8.dolphin.ui;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fire8.dolphin.service.JaberClientService;
import com.fire8.dolphin.service.xmpp.MessageProcessHandler;
import com.fire8.dolphin.utils.LogUtil;

public class OpenfireClientActivity extends Activity {

	private JaberClientService jaberClientService;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.openfire_client);
		// 初始化按钮事件
		jaberClientService = JaberClientService.getInstance();
		bindComponentEvent();
	}

	private void bindComponentEvent() {
		
		MessageProcessHandler processHandler = new MessageProcessHandler() {

			@Override
			public void onBeAddFriend(final Presence presence) {
				LogUtil.d("onBeAddFriend.........");
				Builder builder = new AlertDialog.Builder(OpenfireClientActivity.this);
				builder.setIcon(R.drawable.answer);
				builder.setTitle("来自[" +  presence.getFrom() + "]的好友邀请");
				builder.setMessage("加我为好友啊，来吧");
				builder.setPositiveButton("同意", new AlertDialog.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						jaberClientService.responseToAddFriend( presence.getFrom(), true);
					}
				});
				
				builder.setNegativeButton("拒绝", new AlertDialog.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						jaberClientService.responseToAddFriend( presence.getFrom(), false);
					}
				});
				builder.create().show();
				
			}

			@Override
			public void onAddFriendRefused(Presence presence) {
				LogUtil.d("加好友被拒绝了...");
				Toast.makeText(OpenfireClientActivity.this, "你被拒绝了", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onChatMsg(Message msg) {
				LogUtil.d("收到好友消息...");
				String s = msg.getFrom() + ": " + msg.getBody();
				Toast.makeText(OpenfireClientActivity.this, s, Toast.LENGTH_SHORT).show();
			}
			
		};
		jaberClientService.addMessageListener(processHandler);
		
		//注册
		Button regBtn = (Button) this.findViewById(R.id.registerBtn);
		regBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText u = (EditText) OpenfireClientActivity.this.findViewById(R.id.usernameTxt);
				 String username = u.getText().toString().trim();
				 LogUtil.d("username::::注册:" + username);
				 try {
					jaberClientService.connect();
					jaberClientService.register(username, "123456");
				} catch (XMPPException e) {
					LogUtil.e("连接openfire服务器失败", e);
				}
			}
		});
		
		//登录
		Button loginBtn = (Button) this.findViewById(R.id.loginBtn);
		loginBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				LogUtil.d("username:::: login...");
				EditText u = (EditText) OpenfireClientActivity.this.findViewById(R.id.usernameTxt);
				 String username = u.getText().toString().trim();
				 LogUtil.d("username::::" + username);
				 try {
					jaberClientService.connect();
					jaberClientService.login(username, "123456");
				} catch (XMPPException e) {
					LogUtil.e("连接openfire服务器失败", e);
				}
			}
		});
		
		//增加好友
		Button addFriendBtn = (Button) this.findViewById(R.id.addFriendBtn);
		addFriendBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText u = (EditText) OpenfireClientActivity.this.findViewById(R.id.addFriendTxt);
				 String username = u.getText().toString().trim();
				 LogUtil.d("加好友..."  + username);
				 try {
					jaberClientService.requestAddFriend(username);
				} catch (XMPPException e) {
					LogUtil.e("加好友异常", e);
				}
			}
		});
		
		//发送消息
		Button sendMsgBtn = (Button) this.findViewById(R.id.sendMsgBtn);
		sendMsgBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText u = (EditText) OpenfireClientActivity.this.findViewById(R.id.addFriendTxt);
				String username = u.getText().toString().trim();
				
				EditText u2 = (EditText) OpenfireClientActivity.this.findViewById(R.id.msgTxt);
				String msg = u2.getText().toString().trim();
				try {
					jaberClientService.sendMessage(username, msg);
				} catch (XMPPException e) {
					LogUtil.e("发送消息异常了", e);
				}
			}
		});
		
		//断开与openfire的连接
		Button disConnBtn = (Button) this.findViewById(R.id.disConnBtn);
		disConnBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					jaberClientService.disConn();
				} catch (Exception e) {
					LogUtil.e("断开连接异常", e);
				}
			}
		});

		//断开与openfire的连接
				Button getFriendBtn = (Button) this.findViewById(R.id.getFriendsBtn);
				getFriendBtn.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						jaberClientService.getFriends();
						jaberClientService.getUserRealName("");
					}
				});
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		jaberClientService.disConn();
	}
	
	
	

}
